'''
 * @ author     ：廖传港
 * @ date       ：Created in 2020/10/29 16:38
 * @ description：
 * @ modified By：
 * @ ersion     : 
 * @File        : test.py 
'''

import numpy as np
import os

import struct

# 加载数据集
def load_mnist(path, kind='train'):
    """Load MNIST data from `path`"""
    #    labels_path = os.path.join(path,
    #                               '%s-labels-idx1-ubyte'
    #                               % kind)
    #    images_path = os.path.join(path,
    #                               '%s-images-idx3-ubyte'
    #                               % kind)

    labels_path = os.path.join(path,
                               '%s-labels.idx1-ubyte'
                               % kind)
    images_path = os.path.join(path,
                               '%s-images.idx3-ubyte'
                               % kind)

    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II',
                                 lbpath.read(8))
        labels = np.fromfile(lbpath,
                             dtype=np.uint8)

    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII',
                                               imgpath.read(16))
        images = np.fromfile(imgpath,
                             dtype=np.uint8).reshape(len(labels), 784)

    print("images---->", images[5])
    print("labels---->", labels[5])

    return images, labels



[train_images, train_labels] = load_mnist('D:/python/data/MNIST')